Git CheatSheet 💻
Command | Description | How to Use | Example |
git init | Initializes a new Git repository | git init | git init (inside the project folder) |
git clone | Creates a local copy of a remote repository | git clone <repo_url> | git clone <https://github.com/user/repo.git > |
git status | Shows the status of changes in the working directory | git status | git status |
git add | Stages changes for the next commit | git add <file_or_directory> | git add . (to stage all changes) |
git commit | Commits staged changes with a message | git commit -m "<commit_message>" | git commit -m "Initial commit" |
git log | Displays the commit history | git log | git log --oneline (compact view of commits) |
git diff | Shows differences between working directory and index | git diff | git diff (shows unstaged changes) |
git branch | Lists, creates, or deletes branches | git branch | git branch feature-branch (to create a new branch) |
git checkout | Switches between branches | git checkout <branch_name> | git checkout master (switch to master branch) |
git merge | Merges changes from one branch into another | git merge <branch_name> | git merge feature-branch |
git pull | Fetches and merges changes from the remote repository | git pull | git pull origin master |
git push | Pushes committed changes to a remote repository | git push <remote> <branch_name> | git push origin master |
git remote | Manages connections to remote repositories | git remote -v | git remote add origin <https://github.com/user/repo.git > |
git fetch | Downloads changes from a remote repository without merging | git fetch <remote> | git fetch origin |
git reset | Unstages or resets changes to a previous commit | git reset <file> or git reset --hard | git reset --hard HEAD (resets all changes to the last commit) |
git rm | Removes a file from the working directory and staging area | git rm <file> | git rm file.txt |
git stash | Temporarily saves changes that aren’t ready for commit | git stash | git stash (saves changes) |
git stash apply | Applies the most recent stash | git stash apply | git stash apply |
git tag | Creates a tag to mark specific commits | git tag <tag_name> | git tag v1.0.0 |
git show | Shows details about a specific commit or tag | git show <commit/tag> | git show v1.0.0 |
git log --graph | Displays commit history in a graph format | git log --graph | git log --graph --oneline |
git cherry-pick | Applies the changes from a specific commit to the current branch | git cherry-pick <commit_hash> | git cherry-pick a1b2c3d4 |
git rebase | Reapplies commits on top of another base tip | git rebase <branch> | git rebase master |
git revert | Reverts a specific commit by creating a new commit | git revert <commit_hash> | git revert a1b2c3d4 |
git blame | Shows which commits modified each line of a file | git blame <file> | git blame README.md |
git bisect | Finds the commit that introduced a bug using binary search | git bisect start / git bisect good <commit> / git bisect bad | Start a bisect search |
git clean | Removes untracked files from the working directory | git clean -f | git clean -f (forces removal of untracked files) |
git config | Configures Git settings (username, email, etc.) | git config <key> <value> | git config --global user.name "John Doe" |
git archive | Creates a zip or tarball of the repository | git archive --format=zip HEAD > archive.zip | git archive --format=zip HEAD > archive.zip |
git submodule | Manages external repositories as submodules | git submodule add <repo_url> | git submodule add <https://github.com/user/submodule.git > |
git reflog | Shows a log of all changes to the HEAD pointer | git reflog | git reflog |
git ls-files | Lists all the files being tracked by Git | git ls-files | git ls-files |
git commit --amend | Modifies the last commit | git commit --amend -m "<new_message>" | git commit --amend -m "Updated commit message" |